package cn.merryyou.security.security;

import org.mockito.internal.util.collections.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Created by hxy on 2018/6/20
 * E-mail:hxyHelloWorld@163.com
 * github:https://github.com/haoxiaoyong1014
 */
@Component
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private PasswordEncoder passwordEncoder;


    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Set<String> authorityIdSet = Sets.newSet("ROLE_USER");
        Function<String, GrantedAuthority> function = s -> (GrantedAuthority) () -> s;
        Set<GrantedAuthority> grantedAuthorities = authorityIdSet.stream().map(function).collect(Collectors.toSet());
/*
        List<GrantedAuthority> role_user = AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USE");

        return new User(username, passwordEncoder.encode("123456"), role_user
       */
        return new User(username, passwordEncoder.encode("123456"), true, true, true, true, grantedAuthorities
        );

    }
}
